<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="量表标题：" prop="scaleTitleShow" label-width="7rem">
        <el-input
          v-model="queryParams.scaleTitleShow"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="部门：" prop="deptName" label-width="6rem">
        <el-input
          v-model="queryParams.deptName"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!-- <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:result_personal:edit']"
        >详情</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:result_personal:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table class="ellipsis" v-loading="loading" :data="result_personalList" @selection-change="handleSelectionChange" tooltip-effect="dark myTooltips">
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="序号" align="center" prop="resultId" /> -->
      <!-- <el-table-column label="测评量表id" align="center" prop="scaleId" /> -->
      <!-- <el-table-column label="用户id" align="center" prop="userId" /> -->
      <el-table-column label="用户姓名" align="center" prop="loginName" />
      <el-table-column label="登录账号" align="center" prop="userName" />
      <el-table-column label="部门" align="center" prop="deptName" />
      <el-table-column label="量表名称" align="center" prop="scaleTitleShow" />
      <!-- <el-table-column label="班级" align="center" prop="classesName" /> -->
      <el-table-column label="提交时间" align="center" prop="startTime" />
      <el-table-column label="报告授权" align="center" prop="report"  v-if="roleName !== '学生'">
        <template slot-scope="scope">
          <el-button size="mini"
           type="text"
           icon="el-icon-edit"
           :disabled="scope.row.report === '已授权'"
           :class="{'red-button': scope.row.report === '已授权', 'disabled-button': scope.row.report === '已授权'}"
           @click="scope.row.report === '否' ? gotoReport(scope.row) : null">
            {{ scope.row.report === '是' ? '已授权' : '未授权' }}
          </el-button>
        </template>
      </el-table-column>
      <!-- <el-table-column label="总分" align="center" prop="countScore" /> -->
      <!-- <el-table-column label="因子分数" align="center" prop="factorScore" /> -->
      <!-- <el-table-column label="分数范围和结果提示" align="center" prop="resultContent" :show-overflow-tooltip=true /> -->
      <!-- <el-table-column label="因子的分数范围及结果提示" align="center" prop="factorResult" :show-overflow-tooltip=true /> -->
      <!-- <el-table-column label="用户选择数据" align="center" prop="userAnswer" :show-overflow-tooltip=true /> -->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="gotoIReport(scope.row)">查看</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:result_personal:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改个人测评成绩报告对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="55rem" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="7rem">
        <el-form-item label="测评量表id：" prop="scaleId">
          <el-input v-model="form.scaleId" placeholder="请输入测评量表id" />
        </el-form-item>
        <el-form-item label="量表标题：" prop="scaleTitleShow">
          <el-input v-model="form.scaleTitleShow" placeholder="请输入测评量表标题" />
        </el-form-item>
        <el-form-item label="用户id：" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入用户id" />
        </el-form-item>
        <el-form-item label="登录账号：" prop="loginName">
          <el-input v-model="form.loginName" placeholder="请输入登录账号" />
        </el-form-item>
        <el-form-item label="用户昵称：" prop="userName">
          <el-input v-model="form.userName" placeholder="请输入用户昵称" />
        </el-form-item>
        <!-- <el-form-item label="班级：" prop="classesName">
          <el-input v-model="form.classesName" placeholder="请输入班级" />
        </el-form-item> -->
        <!-- <el-form-item label="学院：" prop="deptName">
          <el-input v-model="form.deptName" placeholder="请输入学院" />
        </el-form-item> -->
        <el-form-item label="总分：" prop="countScore">
          <el-input v-model="form.countScore" placeholder="请输入总分" />
        </el-form-item>
        <el-form-item label="因子的分数：" prop="factorScore">
          <el-input v-model="form.factorScore" placeholder="请输入因子的分数" />
        </el-form-item>
        <el-form-item label="用户选择数据：" prop="userAnswer">
          <el-input v-model="form.userAnswer" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="分数范围和结果提示：" prop="resultContent">
          <el-input v-model="form.resultContent" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="因子的分数范围及结果提示：" prop="factorResult">
          <el-input v-model="form.factorResult" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">关 闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listResult_personal, getResult_personal, delResult_personal, addResult_personal, updateResult_personal } from "@/api/mentalTest/personScore.js";
import { getInfo } from "@/api/login"
export default {
  name: "Result_personal",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 个人测评成绩报告表格数据
      result_personalList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      //获取总用户id
      userID:null,
      //判断登录者的身份
      roleName:null,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        scaleId: null,
        scaleTitleShow: null,
        userId: null,
        loginName: null,
        userName: null,
        countScore: null,
        factorScore: null,
        resultContent: null,
        factorResult: null,
        userAnswer: null,
        classesName: null,
        deptName: null,
        startTime:null,
        report:null,
        riskLevel:10,
      },
      // 表单参数
      form: {
        resultId: null,//测评结果id
        scaleId: null,//测评量表id
        scaleTitleShow: null,//测评量表名称
        userId: null,//用户id
        loginName: null,//登录账号
        userName: null,//用户昵称
        countScore: null,//总分
        factorScore: null,//因子的分数
        resultContent: null,//分数范围和结果提示
        factorResult: null,//因子的分数范围及结果提示
        userAnswer: null,//用户选择数据
        createTime: null,//创建时间
        updateTime: null,//更新修改时间
        classesName: null,//班级
        deptName: null,//学院
        startTime:null,
        report:null,
      },
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  mounted() {
    this.getRole();
  },
  methods: {
    /** 查询个人测评成绩报告列表 */
    async getList() {
      this.loading = true;
      await getInfo().then(response1 =>{
        this.userID = response1.user.userId
        console.log("2222",response1.user.userId);
        console.log(this.userID);
      });
      // const userId = this.$route.params && this.$route.params.userId;
      console.log("1111",this.userID);
      this.queryParams.userId = this.userID;
      console.log("请求参数1",this.queryParams);
      listResult_personal(this.queryParams).then(response => {
        console.log("请求参数2",this.queryParams);
        this.result_personalList = response.rows;
        console.log("res",response.rows);
        console.log("result_personalList",this.result_personalList);
        this.total = response.total;
        this.loading = false;
      });
    },
    //获取登录者的身份
    getRole(){
      getInfo().then(response =>{
        console.log("身份",response.user.roles[0].roleName);
        this.roleName = response.user.roles[0].roleName;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        resultId: null,//测评结果id
        scaleId: null,//测评量表id
        scaleTitleShow: null,//测评量表名称
        userId: null,//用户id
        loginName: null,//登录账号
        userName: null,//用户昵称
        countScore: null,//总分
        factorScore: null,//因子的分数
        resultContent: null,//分数范围和结果提示
        factorResult: null,//因子的分数范围及结果提示
        userAnswer: null,//用户选择数据
        createTime: null,//创建时间
        updateTime: null,//更新修改时间
        classesName: null,//班级
        deptName: null//学院
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.resultId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加个人测评成绩报告";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const resultId = row.resultId || this.ids
      getResult_personal(resultId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改个人测评成绩报告";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.resultId != null) {
            updateResult_personal(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addResult_personal(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const resultIds = row.resultId || this.ids;
      this.$modal.confirm('是否确认删除个人测评成绩报告编号为"' + resultIds + '"的数据项？').then(function() {
        return delResult_personal(resultIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.$modal.confirm('确认导出所有测评成绩报告信息吗？').then(response => {
        this.download('system/result_personal/export', {
          ...this.queryParams
        }, `result_personal_${new Date().getTime()}.xlsx`)
      })
    },
    //跳转到报告
    gotoIReport(row) {
      this.$router.push({
        path: '/mentalTest/personReport',
        query: {
            resultId: row.resultId,
        }
      })
    },
    //分配自杀风险量表
    gotoReport(row){
      const resultIds = row.resultId || this.ids;
      console.log("resultIds",resultIds);
      getResult_personal(resultIds).then(response => {
        console.log("data",response.data);
        // this.form = response.data;
        this.form = JSON.parse(JSON.stringify(response.data));
        console.log('this.form',this.form);
      });
      console.log('外面',this.form);
      this.$confirm('是否确认授权？', '信息', {
        confirmButtonText: '确定',
        cancelButtonText: '取消', // 添加取消按钮
        showCancelButton: true, // 显示取消按钮
      }).then(()=> {
        this.form.report = '是';
        console.log("form11",this.form);
        updateResult_personal(this.form).then(response => {
            console.log("修改后的",response.data);
            this.$modal.msgSuccess("修改成功");
            // this.open = false;
            // this.getList();
          });
        }).catch(() => {
          this.$message({
            message: '已取消授权'
          });          
        });
    },
  }
};
</script>
<style>
.myTooltips{
  background-color: rgb(108, 195, 230)!important;
   color: #fff;
   border: 1px solid #333;
   max-width: 30%;
   text-indent:2em;
}
</style>

